package io.renren.modules.pachong.util;

import io.renren.modules.pachong.Movie;
import io.renren.modules.pachong.MovieTag;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Peter
 * @date 2018-8-10
 */
public class SiHaiDYhtmlParser {


    public static String URL = "http://www.sihaidy.com";


    public static List<Movie> addMoive() throws IOException {
        Map<String,Map<String,Long>> tagMap = new HashMap<>();
        tagMap.put("2",new HashMap<>());
        tagMap.put("4",new HashMap<>());
        tagMap.put("5",new HashMap<>());
        List<Movie> list = new ArrayList<>();
        // 分页
        for (int i = 1; i <= 4; i++) {
            Document doc = Jsoup.connect("http://www.sihaidy.com/list/1_" + i + ".html").get();
            Elements ul_all = doc.getElementsByAttributeValue("data-am-widget", "gallery");
            if (ul_all.size() == 0) {
                continue;
            }
            Element ul = ul_all.get(0);
            if (ul == null) {
                continue;
            }
            Elements lis = ul.getElementsByTag("li");
            int len = lis.size();
            // 处理每个电影
            for (int j = 0; j < len; j++) {
                Element li = lis.get(j);
                Element a = li.getElementsByTag("a").get(0);
                String posterUrl = a.getElementsByTag("img").get(0).attr("src");
                String movieName = a.getElementsByTag("h3").get(0).text();
                String detailHtmlUrl = URL + a.attr("href");
                // 详情页
                Document detail_doc = Jsoup.connect(detailHtmlUrl).get();
                Element div = detail_doc.getElementsByAttributeValue("class", "url").get(0);
                Element playA = div.getElementsByTag("a").get(0);
                String playDetailUrl = "http:" + playA.attr("href");
                // 播放页
                Document play_doc = Jsoup.connect(playDetailUrl).get();
                Element iframe = play_doc.getElementsByTag("iframe").first();
                if (iframe == null) {
                    continue;
                }
                String playUrl = iframe.attr("src");
                Movie m = new Movie();
                m.setName(movieName);
                m.setPlayUrl(playUrl);
                m.setPosterUrl(posterUrl);
                Elements trs = detail_doc.getElementsByTag("tr");
                if (trs != null) {//2.4.5
                    for (int k = 0; k < trs.size(); k++) {
                        if (k > 5) {
                            break;
                        }
                        // 类型
                        if (k == 2) {
                            Element tr = trs.get(k);
                            Elements as =  tr.getElementsByTag("td").last().getElementsByTag("a");
                            if(as.size() > 0){
                                String typeStr = "";
                                for(Element e : as){
                                    String val = e.text();
                                    Map<String,Long> map = tagMap.get(k+"");
                                    if(map.containsKey(val)){
                                        Long typeInt = map.get(val);
                                        typeStr += typeInt+",";
                                    }else{
                                        MovieTag tag = new MovieTag();
                                        tag.setPid(1L);
                                        tag.setName(val);
                                        // 插入库，并返回主键

                                        map.put(val,tag.getId());
                                        typeStr += tag.getId()+",";
                                    }
                                }
                                typeStr = typeStr.substring(0,typeStr.length()-1);
                                m.setType(typeStr);
                            }
                        } else if (k == 4) {// 地区
                            m.setCountry(getLastColumOfRow(trs,k));
                        } else if (k == 5) {
                            m.setYear(Integer.valueOf(getLastColumOfRow(trs,k)));
                        }
                    }
                }
                list.add(m);
            }
        }
        return list;
    }

    public static String getLastColumOfRow(Elements rows,int index){
        Element tr = rows.get(index);
        return tr.getElementsByTag("td").last().getElementsByTag("a").last().text();
    }

    public static void main(String[] args) throws IOException {
        addMoive();
    }

}
